package com.example.demo.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年01⽉26⽇<br/>
 * @version：v1.0.0<br>
 */
public class CglibProxy implements MethodInterceptor {

    private Object targetObject;

    public Object newProxy(Object targetObject) {
        this.targetObject = targetObject;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(targetObject.getClass());
        enhancer.setCallback(this);
        Object o = enhancer.create();
        return o;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        checkPopedom();
        Object invoke = method.invoke(targetObject, objects);
        return invoke;
    }

    private void checkPopedom() {
        System.out.println("======检查权限checkPopedom()======");
    }
}
